package mixed;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

public class HomeWork615 {

	/** 主窗体 */
	private JFrame frame;
	/** File -> New 菜单项 */
	private JMenuItem mntmNew;
	/** File -> Open 菜单项 */
	private JMenuItem mntmOpen;
	/** File -> Open 菜单项 */
	private JMenuItem mntmExit;
	/** Edit -> Cut 菜单项 */
	private JMenuItem mntmCut;
	/** Edit -> Copy 菜单项 */
	private JMenuItem mntmCopy;
	/** Edit -> Paste 菜单项 */
	private JMenuItem mntmPaste;

	public static void main(String[] args) {

		// 尝试更改外观为系统外观
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}

		HomeWork615 app = new HomeWork615();
		app.frame.setVisible(true);

	}

	/**
	 * 创建程序
	 */
	public HomeWork615() {
		initialize();
	}

	/**
	 * 初始化窗体
	 */
	private void initialize() {
		frame = new JFrame("练习6.15");
		frame.setBounds(100, 100, 450, 300);
		// 设置右上角叉叉点击为菜单项File->Exit的单击事件
		frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				super.windowClosing(e);
				mntmExit.doClick();
			}
		});

		// 菜单条
		JMenuBar menuBar = new JMenuBar();
		// 设置窗体菜单条
		frame.setJMenuBar(menuBar);

		// File菜单
		JMenu mnFile = new JMenu("File");
		menuBar.add(mnFile);

		mntmNew = new JMenuItem("New");
		mntmNew.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		// 添加到 File 菜单
		mnFile.add(mntmNew);

		mntmOpen = new JMenuItem("Open");
		mntmOpen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser jfc = new JFileChooser(".");
				if (jfc.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
					// do works
				}
			}
		});
		// 添加到 File 菜单
		mnFile.add(mntmOpen);

		mntmExit = new JMenuItem("Exit");
		mntmExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (JOptionPane.showConfirmDialog(frame, "Do you really want to exit?", "Confirm Exit", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
					System.exit(0);
				}
			}
		});
		// 添加到 File 菜单
		mnFile.add(mntmExit);

		// Edit菜单
		JMenu mnEdit = new JMenu("Edit");
		menuBar.add(mnEdit);

		mntmCut = new JMenuItem("Cut");
		mntmCut.setEnabled(false);
		mntmCut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		// 添加到Edit菜单
		mnEdit.add(mntmCut);

		mntmCopy = new JMenuItem("Copy");
		mntmCopy.setEnabled(false);
		mntmCopy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		// 添加到Edit菜单
		mnEdit.add(mntmCopy);

		mntmPaste = new JMenuItem("Paste");
		mntmPaste.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		// 添加到Edit菜单
		mnEdit.add(mntmPaste);
	}

}
